"use strict";
/* Controllers */
app.controller("apiTestController", ['$scope', '$location', '$rootScope', '$routeParams', 'apiTestService', function ($scope, $location, $rootScope, $routeParams, apiTestService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    vm.p.apiParamsKey = [];

    vm.p.apiName = $routeParams.apiName;
    vm.p.apis = rbacDefined.apis;
    vm.p.api = rbacDefined.getMap().get(vm.p.apiName);

    for(var i=1;i<6;i++){
        vm.p.apiParamsKey.push(vm.p.apiName+'-params00'+i);
    }

    var errorFn = function (result) {
        $('#loading').modal('hide');
        vm.result = result;
        vm.p.ms = Date.now()-vm.p.ms;
    };

    $scope.search = function () {
        $('#loading').modal('show');
        var vm = $scope.vm;
        vm.p.ms = Date.now();
        var i = 0;
        var params = {};
        for (var key in vm.p.api.params) {
            params[vm.p.api.params[key].name] = $scope.conf[i++];//取出动态model绑定的数据，存入json模版
        }
        var procFn = function (response) {
            vm.result = response;
            $('#loading').modal('hide');
            vm.p.ms = Date.now()-vm.p.ms;
        };
        var url = vm.p.api.url;
        if(params.token!=null) {
            url +=("?plat=1&plat_token=" + params.token);
        }
        apiTestService.search(params, procFn, errorFn, url);
    };
    $scope.setApiParams = function () {
        $('#loading').modal('show');
        var vm = $scope.vm;
        var i = 0;
        var value = [];
        for (var key in vm.p.api.params) {
            value.push({name:vm.p.api.params[key].name,value:$scope.conf[i++]});
        }
        var params = {key:vm.p.apiKey,value:JSON.stringify(value)};

        var procFn = function (response) {
            $('#loading').modal('hide');
        };
        apiTestService.setApiParams(params, procFn, errorFn);
    };

    $scope.getApiParams = function () {
        $('#loading').modal('show');
        var vm = $scope.vm;
        var params = {key:vm.p.apiKey};
        var procFn = function (response) {
            if(response.data&&response.data.value) {
                vm.p.api.params = JSON.parse(response.data.value);
            }
            $scope.buildConf();
            $('#loading').modal('hide');
        };
        apiTestService.getApiParams(params, procFn, errorFn);
    };
    $scope.$watch("vm.p.api", function (newVal, oldVal, scope) {
        $location.path('/hllopen/rbac/api_test/'+vm.p.api.name).replace();
        $scope.buildConf()
    });

    $scope.buildConf = function(){
        $scope.conf = [];
        vm.p.api.params.forEach(function (e, i, a) {
            $scope.conf.push(e.value)
        });
    };
    //$scope.$watch("vm.p.apiKey", function (newVal, oldVal, scope) {
    //    $scope.getApiParams();
    //});

}]);


/* Services */
app.service('apiTestService', ['ajaxService', function (ajaxService) {

    this.search = function (params, successFunction, errorFunction, url) {
        ajaxService.AjaxPost(params, ".." + url, successFunction, errorFunction);
    };
    this.setApiParams = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "../audit/apiTest/setApiParams.svc" , successFunction, errorFunction);
    };
    this.getApiParams = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "../audit/apiTest/getApiParams.svc" , successFunction, errorFunction);
    };
}]);
